package com.zlc.common.util.autocode;

import com.zlc.common.common.entity.BaseEntity;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author: zhaoxinliang
 * @date: 2021/8/20 10:27
 * @description: 字段校验生成配置
 */
public class FiledValid {
    /**
     * @Description: 非空统一过滤字段
     */
    public static final String[] COMMON_NOTNULL_SKIP_FILED = {"id", "version", "createUser", "createTime", "updateUser",
        "updateTime", "deleteFlag", "processInstanceId", "projectStage", "processInstanceNo"};
    /**
     * @Description: 不生成字段定义
     * @return:
     * @Author: zhaoxinliang
     * @date: 2021/8/20
     */
    private static final Field[] BASE_FIELDS = BaseEntity.class.getDeclaredFields();

    public static boolean skipFileds(String columnName) {
        return Arrays.stream(BASE_FIELDS)
                .anyMatch(a -> a.getName().equals(CodeDOM.StringUtil.camelCaseName(columnName)));
    }

    /**
     * @param tableFieldInfos
     * @param tableName
     * @Description: 字段校验设置 (采用jpa+he)
     * @return:
     * @Author: zhaoxinliang
     * @date: 2021/8/20
     */
    public static List<String> getInvalidFiled(List<CodeDOM.TableFieldInfo> tableFieldInfos, String tableName) {
        // 不能为空
        Stream<CodeDOM.TableFieldInfo> infoStream =
            tableFieldInfos.stream().filter(tableFieldInfo -> StringUtils.equals("NO", tableFieldInfo.getIsNullAble())
                && !tableFieldInfo.getColumnName().contains("_flag"));

        // 需要生成校验字段
        return infoStream.map(CodeDOM.TableFieldInfo::getColumnName)
            .filter(columName -> !Arrays.asList(
                ArrayUtils.addAll(BasePath.TableToPath.getSkipFiledByTableName(tableName), COMMON_NOTNULL_SKIP_FILED))
                .contains(CodeDOM.StringUtil.camelCaseName(columName)))
            .collect(Collectors.toList());
    }

}
